package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.io.IOException;

public class LoginPage extends Utils {
    public static String url = "http://127.0.0.1:8080/login.html";
    public static String res = "";

    public LoginPage() {
        super(url);
    }

    //检查页面是否加载成功
    public  void loginPageRight() throws InterruptedException, IOException {
        //通过查看页面元素是否存在来检查页面加载成功与否
        driver.findElement(By.cssSelector("body > div.login-container > div > h3"));
        getScreenShot(getClass().getName());
    }

    //检查基本控件
    //检查输入框是否可以正常输入、不输入文字点击登录按钮
    public void BasicTest() throws InterruptedException, IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("111");
        String edit = driver.findElement(By.cssSelector("#username")).getAttribute("value");
        getScreenShot(getClass().getName());
        assert edit.equals("111");

        driver.findElement(By.cssSelector("#password")).sendKeys("11122");
        edit = driver.findElement(By.cssSelector("#password")).getAttribute("value");
        getScreenShot(getClass().getName());
        assert edit.equals("11122");

        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#submit")).click();
        Alert alert = driver.switchTo().alert();
        res = alert.getText();
//        getScreenShot(getClass().getName());
        assert res.equals("当前输入的用户名或者密码为空!");
        alert.accept();
    }

    //登陆失败检查
    public void LoginErr() throws IOException {
        //不输入账号尝试登录
        driver.findElement(By.cssSelector("#password")).sendKeys("111");
        driver.findElement(By.cssSelector("#submit")).click();
        Alert alert = driver.switchTo().alert();
        res = alert.getText();
//        getScreenShot(getClass().getName());
        assert res.equals("当前输入的用户名或者密码为空!");
        alert.accept();
        driver.findElement(By.cssSelector("#password")).clear();

        //不输入密码尝试登录
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#submit")).click();
        alert = driver.switchTo().alert();
        res = alert.getText();
//        getScreenShot(getClass().getName());
        assert res.equals("当前输入的用户名或者密码为空!");
        alert.accept();
        driver.findElement(By.cssSelector("#username")).clear();
    }

    //成功登录
    public void LoginSuc() throws InterruptedException, IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(3000);
        Alert alert = driver.switchTo().alert();
        res = alert.getText();

        assert res.equals("登陆成功！");
        alert.accept();

        driver.navigate().back();
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys("lisi");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(500);
        alert = driver.switchTo().alert();
        res = alert.getText();
        assert res.equals("登陆成功！");
        alert.accept();
    }
}
